package io.renren.modules.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.product.entity.ProductUserEntity;
import io.renren.modules.product.mapper.ProductUserMapper;
import io.renren.modules.product.service.ProductUserService;
import io.renren.modules.product.vo.TransferVo;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.vo.EmployeeVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoyu
 * @since 2023-07-29
 */
@Service
public class ProductUserServiceImpl extends ServiceImpl<ProductUserMapper, ProductUserEntity> implements ProductUserService {
    @Autowired
    SysUserService sysUserService;

    /**
     * 获取成员管理穿梭框的左右两边的数据
     * @param productId
     * @return
     */
    @Override
    public Map<String, Object> getTransfer(Long productId) {
        //查询当前产品下有哪些员工
        List<Long> productEmps = baseMapper.getIsThisProductEmp(productId);
        //获取穿梭框左边（未加入该产品的员工）
        List<EmployeeVo> allEmployee = sysUserService.getAllEmployee();
        List<TransferVo> transferLeft = allEmployee.stream()
                //.filter(employeeVo -> !productEmps.contains(employeeVo.getUserId()))
                .map(employeeVo -> {
                    TransferVo transferVo = new TransferVo();
                    transferVo.setUserId(employeeVo.getUserId());
                    transferVo.setUserWithRole(employeeVo.getUsername() + "\t" + employeeVo.getRoleName());
                    return transferVo;
                }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("transferLeft",transferLeft);
        map.put("trasferRight",productEmps);
        return map;
    }

    /**
     * 更新产品下团队成员信息
     * @param productId
     * @param userIds
     */
    @Override
    public void updateProductUser(Long productId, Long[] userIds) {
        this.remove(new LambdaQueryWrapper<ProductUserEntity>()
                .eq(ProductUserEntity::getProductId,productId));
        if (userIds.length > 0){
            for (Long userId : userIds) {
                ProductUserEntity productUserEntity = new ProductUserEntity();
                productUserEntity.setProductId(productId);
                productUserEntity.setUserId(userId);
                this.save(productUserEntity);
            }
        }
    }

    @Override
    public List<ProductUserEntity> getUserByProductId(Long productId) {
        return this.list(new LambdaUpdateWrapper<ProductUserEntity>()
                .eq(ProductUserEntity::getProductId,productId));

    }

    @Override
    public List<Long> getProductIdsByUserId() {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        Long userId = sysUserEntity.getUserId();
        List<ProductUserEntity> productUserEntityList = this.list(new LambdaQueryWrapper<ProductUserEntity>()
                .eq(ProductUserEntity::getUserId, userId));
        List<Long> productIds = productUserEntityList
                .stream().map(productUserEntity -> productUserEntity.getProductId())
                .collect(Collectors.toList());
        return productIds;


    }
}
